package com.itheima.demo1;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

@WebServlet("/studentServlet02")
public class Servlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        String realPath = getServletContext().getRealPath("/student.txt");
        BufferedReader br = new BufferedReader(new FileReader(realPath));
        String s;
        response.getWriter().write("<table align='canter' border='2' style='text-align: center' width='200px'>" +
                "    <tr>" +
                "        <th>姓名</th>" +
                "        <th>年龄</th>" +
                "        <th>成绩</th>" +
                "    </tr>"
        );
        while ((s=br.readLine())!=null){
            String[] split = s.split(":");
            response.getWriter().write("<tr>" +
                    "<td>"+split[0]+"</td>"+
                    "<td>"+split[1]+"</td>"+
                    "<td>"+split[2]+"</td>"+
                    "</tr>");
        }
        response.getWriter().write("</table>");
        br.close();
    }
}
